﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HumanWorkflow.SemanticModel;

namespace HumanWorkflow.LexicalModel
{
    public class LexicalModelContext
    {
        public LexicalModelContext()
        {
            WorkflowToken = new WorkflowToken();
            StepTokens = new List<StepToken>();
        }

        public WorkflowToken WorkflowToken {get; set;}

        public List<StepToken> StepTokens {get; set;}

        public HumanWorkflowSemanticModel TransformToSemanticModel()
        {
            HumanWorkflowSemanticModel semanticModel = new HumanWorkflowSemanticModel();
            
            semanticModel.Name = WorkflowToken.Name;

            Step newStep;

            foreach (StepToken stepToken in StepTokens)
            {
                switch (stepToken.StepType)
                { 
                    case StepTypeEnum.Initial:
                        newStep = new InitialStep { Name = stepToken.Name };
                        break;

                    case StepTypeEnum.HumanWork:
                        newStep = new ManualStep {Name = stepToken.Name, Role = stepToken.Role};
                        break;

                    case StepTypeEnum.SystemCommand:
                        newStep = new AutomaticStep {Name = stepToken.Name};
                        break;

                    case StepTypeEnum.Decision:
                        newStep = new DecisionStep {Name = stepToken.Name};
                        break;

                    case StepTypeEnum.Final:
                        newStep = new FinalStep {Name = stepToken.Name};
                        break;

                    default:
                        throw new Exception("Not supported step type.");
                }

                semanticModel.AddStep(newStep);
            }

            foreach (StepToken stepToken in StepTokens)
            {
                foreach (TransitionToken transitionToken in stepToken.OutTransitions)
                {
                    semanticModel.AddTransition(stepToken.Name, transitionToken.ToStepName, transitionToken.Name);
                }
            }

            return semanticModel;

        }

    }
}
